package com.producteev4j.services.version0;

import com.producteev4j.model.json.dashboard.DashBoardListWrapperImpl;
import com.producteev4j.model.json.dashboard.DashBoardWrapperImpl;
import com.producteev4j.transport.ProducteevTransport;
import com.producteev4j.exceptions.ProducteevException;
import com.producteev4j.model.json.task.TaskListWrapperImpl;
import com.producteev4j.model.json.user.UserListWrapperImpl;
import com.producteev4j.model.request.ProducteevGetRequest;
import com.producteev4j.model.request.ProducteevRequest;
import com.producteev4j.model.response.*;

import java.util.Date;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: jcarrey
 * Date: 3/07/11
 * Time: 11:39
 * To change this template use File | Settings | File Templates.
 */
public class ProducteevDashboardServiceV0Impl implements ProducteevDashboardService {

    private static final String DASHBOARD_CONTEXT = "/dashboards";

    private static final String CREATE_ENDPOINT = ProducteevServiceV0.PRODUCTEEV_API_ENDPOINT + DASHBOARD_CONTEXT + "/create.json";
    private static final String VIEW_ENDPOINT = ProducteevServiceV0.PRODUCTEEV_API_ENDPOINT + DASHBOARD_CONTEXT + "/view.json";
    private static final String SHOW_LIST_ENDPOINT = ProducteevServiceV0.PRODUCTEEV_API_ENDPOINT + DASHBOARD_CONTEXT + "/show_list.json";
    private static final String ACCESS_ENDPOINT = ProducteevServiceV0.PRODUCTEEV_API_ENDPOINT + DASHBOARD_CONTEXT + "/access.json";
    private static final String LEAVE_ENDPOINT = ProducteevServiceV0.PRODUCTEEV_API_ENDPOINT + DASHBOARD_CONTEXT + "/leave.json";
    private static final String SET_TITLE_ENDPOINT = ProducteevServiceV0.PRODUCTEEV_API_ENDPOINT + DASHBOARD_CONTEXT + "/set_title.json";
    private static final String SET_SMART_LABEL_ENDPOINT = ProducteevServiceV0.PRODUCTEEV_API_ENDPOINT + DASHBOARD_CONTEXT + "/set_smart_labels.json";
    private static final String DELETE_ENDPOINT = ProducteevServiceV0.PRODUCTEEV_API_ENDPOINT + DASHBOARD_CONTEXT + "/delete.json";
    private static final String TASKS_ENDPOINT = ProducteevServiceV0.PRODUCTEEV_API_ENDPOINT + DASHBOARD_CONTEXT + "/tasks.json";
    private static final String CONFIRM_ENDPOINT = ProducteevServiceV0.PRODUCTEEV_API_ENDPOINT + DASHBOARD_CONTEXT + "/confirm.json";
    private static final String REFUSE_ENDPOINT = ProducteevServiceV0.PRODUCTEEV_API_ENDPOINT + DASHBOARD_CONTEXT + "/refuse.json";
    private static final String INVITE_USER_BY_ID_ENDPOINT = ProducteevServiceV0.PRODUCTEEV_API_ENDPOINT + DASHBOARD_CONTEXT + "/invite_user_by_id.json";
    private static final String INVITE_USER_BY_EMAIL_ENDPOINT = ProducteevServiceV0.PRODUCTEEV_API_ENDPOINT + DASHBOARD_CONTEXT + "/invite_user_by_email.json";
    private static final String NEED_UPGRADE_LIST_ENDPOINT = ProducteevServiceV0.PRODUCTEEV_API_ENDPOINT + DASHBOARD_CONTEXT + "/need_upgrade_list.json";
    private static final String NEEDS_UPGRADE_ENDPOINT = ProducteevServiceV0.PRODUCTEEV_API_ENDPOINT + DASHBOARD_CONTEXT + "/needs_upgrade.json";


    private static final String ID_DASHBOARD_PARAM = "id_dashboard";
    private static final String TITLE_PARAM = "title";
    private static final String SINCE_PARAM = "since";
    private static final String PAGE_PARAM = "page";
    private static final String USER_LEAVE_PARAM = "id_user";
    private static final String USER_DELEGATE_PARAM = "id_user_delegate";
    private static final String VALUE_PARAM = "value";
    private static final String ID_USER_INVITE_PARAM = "id_user_to";
    private static final String EMAIL_PARAM = "email";


    @Override
    public DashBoard createDashboard(ProducteevTransport transport, String userToken, String title) throws ProducteevException {
        ProducteevRequest request = new ProducteevGetRequest();
        request.setEndpoint(CREATE_ENDPOINT);
        request.getParams().putValue(TITLE_PARAM, title);
        request.setResponseClass(DashBoardWrapperImpl.class);
        return (ProducteevValidator.assertNoError((DashBoardWrapperImpl) transport.process(request, userToken))).getDashboard();
    }

    @Override
    public DashBoard getDashboard(ProducteevTransport transport, String userToken, long idDashboard) throws ProducteevException {
        ProducteevRequest request = new ProducteevGetRequest();
        request.setEndpoint(VIEW_ENDPOINT);
        request.getParams().putValue(ID_DASHBOARD_PARAM, idDashboard);
        request.setResponseClass(DashBoardWrapperImpl.class);
        return (ProducteevValidator.assertNoError((DashBoardWrapperImpl) transport.process(request, userToken))).getDashboard();

    }

    @Override
    public List<? extends DashBoardWrapper> getDashboards(ProducteevTransport transport, String userToken, Date since, Integer page) throws ProducteevException {
        ProducteevRequest request = new ProducteevGetRequest();
        request.setEndpoint(SHOW_LIST_ENDPOINT);
        if (since != null) {
            request.getParams().putValue(SINCE_PARAM, since);
        }
        if (page != null) {
            request.getParams().putValue(PAGE_PARAM, page);
        }
        request.setResponseClass(DashBoardListWrapperImpl.class);
        return (ProducteevValidator.assertNoError((DashBoardListWrapperImpl) transport.process(request, userToken))).getDashboards();

    }

    @Override
    public List<? extends UserWrapper> getDashboardAccess(ProducteevTransport transport, String userToken, long idDashboard) throws ProducteevException {
        ProducteevRequest request = new ProducteevGetRequest();
        request.setEndpoint(ACCESS_ENDPOINT);
        request.getParams().putValue(ID_DASHBOARD_PARAM, idDashboard);
        request.setResponseClass(UserListWrapperImpl.class);
        return (ProducteevValidator.assertNoError((UserListWrapperImpl) transport.process(request, userToken))).getAccesslist();

    }

    @Override
    public DashBoard leaveOrDelegateDashboard(ProducteevTransport transport, String userToken, long idDashboard, Long idUser, Long idDelegate) throws ProducteevException {
        ProducteevRequest request = new ProducteevGetRequest();
        request.setEndpoint(LEAVE_ENDPOINT);
        request.getParams().putValue(ID_DASHBOARD_PARAM, idDashboard);
        if (idUser != null) {
            request.getParams().putValue(USER_LEAVE_PARAM, idUser);
        }
        if (idDelegate != null) {
            request.getParams().putValue(USER_DELEGATE_PARAM, idDelegate);
        }
        request.setResponseClass(DashBoardWrapperImpl.class);
        return (ProducteevValidator.assertNoError((DashBoardWrapperImpl) transport.process(request, userToken))).getDashboard();

    }

    @Override
    public DashBoard setTitle(ProducteevTransport transport, String userToken, long idDashboard, String title) throws ProducteevException {
        ProducteevRequest request = new ProducteevGetRequest();
        request.setEndpoint(SET_TITLE_ENDPOINT);
        request.getParams().putValue(ID_DASHBOARD_PARAM, idDashboard);
        request.getParams().putValue(TITLE_PARAM, title);
        request.setResponseClass(DashBoardWrapperImpl.class);
        return (ProducteevValidator.assertNoError((DashBoardWrapperImpl) transport.process(request, userToken))).getDashboard();

    }

    @Override
    public DashBoard setSmartLabels(ProducteevTransport transport, String userToken, long idDashboard, int value) throws ProducteevException {
        ProducteevRequest request = new ProducteevGetRequest();
        request.setEndpoint(SET_SMART_LABEL_ENDPOINT);
        request.getParams().putValue(ID_DASHBOARD_PARAM, idDashboard);
        request.getParams().putValue(VALUE_PARAM, value);
        request.setResponseClass(DashBoardWrapperImpl.class);
        return (ProducteevValidator.assertNoError((DashBoardWrapperImpl) transport.process(request, userToken))).getDashboard();

    }

    @Override
    public boolean deleteDashboard(ProducteevTransport transport, String userToken, long idDashboard) throws ProducteevException {
        ProducteevRequest request = new ProducteevGetRequest();
        request.setEndpoint(DELETE_ENDPOINT);
        request.getParams().putValue(ID_DASHBOARD_PARAM, idDashboard);
        request.setResponseClass(BaseResponse.class);
        BaseResponse response = (BaseResponse) transport.process(request, userToken);
        return !(ProducteevValidator.assertNoError(response)).isError() && response.getStats().getResult().toLowerCase().contains("TRUE");

    }

    @Override
    public List<? extends TaskWrapper> getDashboardTasks(ProducteevTransport transport, String userToken, long idDashboard, Date since, Integer page) throws ProducteevException {
        ProducteevRequest request = new ProducteevGetRequest();
        request.setEndpoint(TASKS_ENDPOINT);
        request.getParams().putValue(ID_DASHBOARD_PARAM, idDashboard);
        if (since != null) {
            request.getParams().putValue(SINCE_PARAM, since);
        }
        if (page != null) {
            request.getParams().putValue(PAGE_PARAM, page);
        }
        request.setResponseClass(TaskListWrapperImpl.class);
        return (ProducteevValidator.assertNoError((TaskListWrapperImpl) transport.process(request, userToken))).getTasks();

    }

    @Override
    public DashBoard confirmInvitation(ProducteevTransport transport, String userToken, long idDashboard) throws ProducteevException {
        ProducteevRequest request = new ProducteevGetRequest();
        request.setEndpoint(CONFIRM_ENDPOINT);
        request.getParams().putValue(ID_DASHBOARD_PARAM, idDashboard);

        request.setResponseClass(DashBoardWrapperImpl.class);
        return (ProducteevValidator.assertNoError((DashBoardWrapperImpl) transport.process(request, userToken))).getDashboard();

    }

    @Override
    public DashBoard refuseInvitation(ProducteevTransport transport, String userToken, long idDashboard) throws ProducteevException {
        ProducteevRequest request = new ProducteevGetRequest();
        request.setEndpoint(REFUSE_ENDPOINT);
        request.getParams().putValue(ID_DASHBOARD_PARAM, idDashboard);
        request.setResponseClass(DashBoardWrapperImpl.class);
        return (ProducteevValidator.assertNoError((DashBoardWrapperImpl) transport.process(request, userToken))).getDashboard();

    }

    @Override
    public DashBoard inviteUserById(ProducteevTransport transport, String userToken, long idDashboard, long idUserTo) throws ProducteevException {
        ProducteevRequest request = new ProducteevGetRequest();
        request.setEndpoint(INVITE_USER_BY_ID_ENDPOINT);
        request.getParams().putValue(ID_DASHBOARD_PARAM, idDashboard);
        request.getParams().putValue(ID_USER_INVITE_PARAM, idUserTo);
        request.setResponseClass(DashBoardWrapperImpl.class);
        return (ProducteevValidator.assertNoError((DashBoardWrapperImpl) transport.process(request, userToken))).getDashboard();

    }

    @Override
    public DashBoard inviteUserByEmail(ProducteevTransport transport, String userToken, long idDashboard, String email) throws ProducteevException {
        ProducteevRequest request = new ProducteevGetRequest();
        request.setEndpoint(INVITE_USER_BY_EMAIL_ENDPOINT);
        request.getParams().putValue(ID_DASHBOARD_PARAM, idDashboard);
        request.getParams().putValue(EMAIL_PARAM, email);
        request.setResponseClass(DashBoardWrapperImpl.class);
        return (ProducteevValidator.assertNoError((DashBoardWrapperImpl) transport.process(request, userToken))).getDashboard();

    }

    @Override
    public List<? extends DashBoardWrapper> getDashboardsToUpgrade(ProducteevTransport transport, String userToken) throws ProducteevException {
        ProducteevRequest request = new ProducteevGetRequest();
        request.setEndpoint(NEED_UPGRADE_LIST_ENDPOINT);
        request.setResponseClass(DashBoardListWrapperImpl.class);
        return (ProducteevValidator.assertNoError((DashBoardListWrapperImpl) transport.process(request, userToken))).getDashboards();

    }

    @Override
    public boolean needsUpgrade(ProducteevTransport transport, String userToken, long idDashboard) throws ProducteevException {
        ProducteevRequest request = new ProducteevGetRequest();
        request.setEndpoint(NEEDS_UPGRADE_ENDPOINT);
        request.getParams().putValue(ID_DASHBOARD_PARAM, idDashboard);
        request.setResponseClass(BaseResponse.class);
        BaseResponse response = (BaseResponse) transport.process(request, userToken);
        return !(ProducteevValidator.assertNoError(response)).isError() && response.getStats().getResult().toLowerCase().contains("TRUE");
    }
}
